var bgImg=new Image();//背景对象
bgImg.src='images/bg.jpg';
var bodyImg=new Image();
bodyImg.src='images/body.gif';
var headImg=new Image();
headImg.src='images/l.gif';
var headrImg=new Image();
headrImg.src='images/r.gif';
var headtImg=new Image();
headtImg.src='images/t.gif';
var headdImg=new Image();
headdImg.src='images/b.gif'



function Snake(){
	this.easy=1;//游戏难度
	this.quick=500;
	this.score=document.getElementById('score');
	this.rechange=document.getElementById('rechange');
	this.restart=document.getElementById('restart');
	this.cav=document.getElementById('snake');//获取绘画对象
	this.canvas=this.cav.getContext('2d');//创建绘画环境
	this.width=window.innerWidth;
	this.height=window.innerHeight;//画布高度
	this.cav.width=this.width;
	this.cav.height=this.height;
	this.step=25;//步长
	//this.width=this.width-this.width%this.step;
	//this.height=this.height-this.height%this.step;
	this.stepW=this.width/this.step;//步数
	this.stepH=this.height/this.step;//步数
	this.snakeArr=[];//蛇身数组
	this.foodArr=[];//食物数组
	this.timer=null;//定时器
	this.scores=0;//分数
	this.PcOrPhone=1;//0是电脑 1是手机
	this.init=function(){
		this.score.style.display="block";
		this.checkIsPc();
		this.level();
		this.draw();
		this.run();
	}
	//选择级别
	this.level=function(){
		if(this.easy==1){
			this.quick=500;
		}else if(this.easy==2){
			this.quick=250;
		}else{
			this.quick=50;
		}
	}
	//检测设备是电脑还是手机
	this.checkIsPc=function(){
		var info=navigator.userAgent;
		if(info.indexOf('Macintosh')==13||info.indexOf('Windows')==13){//如果是手机info 字符串返回－1
			this.PcOrPhone=0;
		}
	}
	//1.初始化 界面
	this.draw=function(){
		//1.画背景
		//bgImg.onload=function(){
			this.canvas.drawImage(bgImg,0,0,this.width,this.height);
		//}
		//2.画蛇
		this.drawSnake();
		//3.画食物
		this.drawFood();
	}
	//画食物
	this.drawFood=function(){
		if(this.foodArr.length!=0){
			this.canvas.drawImage(this.foodArr[0].img,this.foodArr[0].x*this.step,this.foodArr[0].y*this.step,this.step,this.step);
			return;
		}
		//食物的x,y坐标
		var x=Math.floor(Math.random()*this.stepW);
		var y=Math.floor(Math.random()*this.stepH);
		 //去除掉与蛇身重复的点
		for(var i=0;i<this.snakeArr.length;i++){
			if(x==this.snakeArr[i].x&&y==this.snakeArr[i].y){
				this.drawFood();
				return;
			}
		}
		//储存食物数据
		this.foodArr[0]={
			x:x,
			y:y,
			img:bodyImg
		}
		this.canvas.drawImage(this.foodArr[0].img,this.foodArr[0].x*this.step,this.foodArr[0].y*this.step,this.step,this.step);
	}
	//画蛇
	this.drawSnake=function(){
		//1.初始画蛇身 5节 x,y,img,d(方向)
		if(this.snakeArr.length<5){
			for(var i=0;i<5;i++){
				this.snakeArr[i]={
					x:Math.floor(this.stepW/2)+i-2,
					y:Math.floor(this.stepH/2),
					img:bodyImg,
					d:'l'
				}
			}
			this.snakeArr[0].img=headImg;
			//console.log(this.snakeArr);
		}
		//2.画蛇（初始化的蛇）以及后续吃食的蛇
		for(var i=0;i<this.snakeArr.length;i++){
			this.canvas.drawImage(this.snakeArr[i].img,this.snakeArr[i].x*this.step,this.snakeArr[i].y*this.step,this.step,this.step);
		}
	}
	//电脑按键事件
	this.keyPress=function(){
		var This=this;
		//按键蛇头数据更新
		document.onkeydown=function(ev){
			var ev =ev ||window.event;
			down=ev.keyCode;//铺获按键元素
			switch(down){
				case 37:
					This.snakeArr[0].d='l';
					This.snakeArr[0].img=headImg;
					break;
				case 38:
					This.snakeArr[0].d='t';
					This.snakeArr[0].img=headtImg;
					break;
				case 39:
					This.snakeArr[0].d='r';
					This.snakeArr[0].img=headrImg;
					break;
				case 40:
					This.snakeArr[0].d='d';
					This.snakeArr[0].img=headdImg;
					break;	
			}
		}

	}
	//手机触摸事件
	this.touchPress=function(){
		var This=this;
		//手机触摸监听事件
		document.addEventListener('touchstart',function(ev){
			if(This.snakeArr.length==5){
				This.snakeArr[0].d=='l';
			}
			var ev =ev ||window.event;
			//获得触摸坐标
			var x = ev.changedTouches[0].clientX/This.step;
			var y = ev.changedTouches[0].clientY/This.step;
			//如果蛇是左右移动的
			if(This.snakeArr[0].d=='l'||This.snakeArr[0].d=='r'){
				if(y>This.snakeArr[0].y){
					This.snakeArr[0].d='d';
					This.snakeArr[0].img=headdImg;
				}else{
					This.snakeArr[0].d='t';
					This.snakeArr[0].img=headtImg;
				}
			}else{//如果蛇是上下移动的
				if(x>This.snakeArr[0].x){
					This.snakeArr[0].d='r';
					This.snakeArr[0].img=headrImg;
				}else{
					This.snakeArr[0].d='l';
					This.snakeArr[0].img=headImg;
				}
			}
		})
		
	}
	//2.让蛇运动
	this.run=function(){
		var This=this;
		if(this.PcOrPhone){
			this.touchPress();
		}else{
			this.keyPress();
		}
		//按键后手机触摸蛇头数据更新
		this.timer=setInterval(function(){
			//1.更新蛇身数据
			for(var i=This.snakeArr.length-1;i>0;i--){
				This.snakeArr[i].x=This.snakeArr[i-1].x;
				This.snakeArr[i].y=This.snakeArr[i-1].y;
			}
			//处理蛇头
			switch(This.snakeArr[0].d){
				case 'l':
					This.snakeArr[0].x--;
					break;
				case 'r':
					This.snakeArr[0].x++;
					break;
				case 't':
					This.snakeArr[0].y--;
					break;
				case 'd':
					This.snakeArr[0].y++;
					break;	
			}
			This.draw();
			This.die();
		},this.quick);
	}
	//3.让蛇去死
	this.die=function(){
		//1.吃到食物
		if(this.snakeArr[0].x==this.foodArr[0].x&&this.snakeArr[0].y==this.foodArr[0].y){
			this.snakeArr.push({
				x:0,
				y:0,
				img:bodyImg
			});
			this.scores++;
			this.score.innerHTML="分数:"+this.scores;
			this.foodArr=[];
		}
		//碰墙壁
		if(this.snakeArr[0].x<0||this.snakeArr[0].x>this.stepW||this.snakeArr[0].y<0||this.snakeArr[0].y>this.stepH){
			this.gameOver();
		}
		//碰到自己
		this.hitSelf=function(){
			for(var i=1;i<this.snakeArr.length;i++){
				if(this.snakeArr[0].x==this.snakeArr[i].x&&this.snakeArr[0].y==this.snakeArr[i].y){
					return true;
				}
			}
			return false;
		}
		if(this.hitSelf()){
			this.gameOver();
		}
	}
	//游戏结束
	this.gameOver=function(){
		this.snakeArr=[];
		this.snakeArr[0]={d:'l'};
		this.foodArr=[];
		clearInterval(this.timer);
		this.drawOver();
		this.scores=0;
		this.PcOrPhone=0;
		this.score.style.display="none";
		this.restart.style.display='block';
		this.rechange.style.display='block';
	}
	//游戏结束界面
	this.drawOver=function(){
		//1.画背景
		this.canvas.drawImage(bgImg,0,0,this.width,this.height);
		//2,写文字
		this.canvas.font = "20px 微软雅黑";
		this.canvas.fillStyle = "black";
		this.canvas.textAlign = "center";
		this.canvas.fillText("不作就不会死",this.width/2,this.height/3);
		this.canvas.font = "20px 微软雅黑";
		this.canvas.fillStyle = "red";
		this.canvas.textAlign = "center";
		this.canvas.fillText("吃掉 " + this.scores +" 个",this.width/2,this.height/2);
	}
	//
}
